"""
Case Type   : tools
Case Name   : 在集群主节点上执行gaussdb指定--single_node启动是否成功
Create At   : 2021/1/18
Owner       : opentestcase003
Description :
    1.关闭正在运行的数据库（主机）
    gs_ctl stop -D /opt/openGauss_zl/cluster/dn1
    2.使用gaussdb工具指定--single_node启动数据库
    gaussdb -D /opt/openGauss_zl/cluster/dn1 -p 19701 --single_node &
    3.查看集群状态是否正常
    gs_om -t status --detail
    4.恢复-重启主数据库
    gs_ctl restart -D /opt/openGauss_zl/cluster/dn1 -M primary
Expect      :
    1.关闭正在运行的数据库成功
    2.使用gaussdb工具指定--single_node启动数据库成功
    3.查看数据库状态成功，集群状态异常
    4.恢复-重启主数据库成功
History     :
    2022-6-15 opentestcase042 优化用例，增加等待时间后判断数据库状态
"""
import unittest
import time
import os
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.userNode = Node(node='PrimaryDbUser')
        self.userNode2 = Node(node='PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()

    def test_systools(self):
        self.logger.info('-------若为单机环境，后续不执行-------')
        excute_cmd = f'source {self.DB_ENV_PATH};gs_om -t status --detail'
        self.logger.info(excute_cmd)
        msg = self.userNode.sh(excute_cmd).result()
        self.logger.info(msg)
        if '6002' not in msg:
            self.logger.info('单机环境，后续不执行!')
        else:
            self.logger.info('--------关闭正在运行的数据库--------')
            excute_cmd1 = f'source {self.DB_ENV_PATH};' \
                          f'gs_ctl stop -D {self.DB_INSTANCE_PATH}'
            self.logger.info(excute_cmd1)
            msg1 = self.userNode.sh(excute_cmd1).result()
            self.logger.info(msg1)
            self.logger.info('--------查看进程，确定关闭成功--------')
            excute_cmd2 = f'ps -ef|grep {self.userNode.ssh_user}'
            self.logger.info(excute_cmd2)
            msg2 = self.userNode.sh(excute_cmd2).result()
            self.logger.info(msg2)
            self.assertFalse(self.DB_INSTANCE_PATH in msg2)
            self.logger.info('----使用gaussdb工具指定--single_node启动数据库---')
            excute_cmd3 = f'source {self.DB_ENV_PATH};' \
                          f'gaussdb -D {self.DB_INSTANCE_PATH} -p' \
                          f' {self.userNode.db_port} --single_node'''
            self.logger.info(excute_cmd3)
            thread_2 = ComThread(self.userNode2.sh, args=(excute_cmd3,))
            thread_2.setDaemon(True)
            thread_2.start()
            thread_2.join(10)
            self.logger.info('----查看集群状态是否正常----')
            excute_cmd3 = f'source {self.DB_ENV_PATH};gs_om -t status --detail'
            self.logger.info(excute_cmd3)
            msg3 = self.userNode.sh(excute_cmd3).result()
            self.logger.info(msg3)
            self.logger.info('----恢复-重启主数据库----')
            excute_cmd4 = f'source {self.DB_ENV_PATH};' \
                          f'gs_ctl restart -D {self.DB_INSTANCE_PATH} ' \
                          f'-M primary'
            self.logger.info(excute_cmd4)
            msg4 = self.userNode.sh(excute_cmd4).result()
            self.logger.info(msg4)
            time.sleep(15)
            self.logger.info('----再次查看集群状态是否恢复----')
            excute_cmd5 = f'source {self.DB_ENV_PATH};gs_om -t status --detail'
            self.logger.info(excute_cmd5)
            msg5 = self.userNode.sh(excute_cmd5).result()
            self.logger.info(msg5)
            self.assertTrue(
                'P Primary Normal' in msg5 and 'S Standby Normal' in msg5)

    def tearDown(self):
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
